package com.szgr.baoyin.fragment;

import android.annotation.TargetApi;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.iflytek.cloud.ErrorCode;
import com.iflytek.cloud.InitListener;
import com.iflytek.cloud.RecognizerResult;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.ui.RecognizerDialog;
import com.iflytek.cloud.ui.RecognizerDialogListener;
import com.szgr.baoyin.MyApplication;
import com.szgr.baoyin.R;
import com.szgr.baoyin.adapter.CustomerListAdapter;
import com.szgr.baoyin.utils.BitmapBlurHelper;
import com.szgr.baoyin.utils.BitmapUtils;
import com.szgr.baoyin.utils.Constant;
import com.szgr.baoyin.utils.JsonParser;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * 客服
 * Created by Jimbo on 2015/9/16.
 */
public class CustomerServiceFragment extends Fragment implements View.OnClickListener, RecognizerDialogListener {
    private RecognizerDialog mSpeechDialog;
    private Map<String,String> mIatResults = new HashMap<>();
    private EditText et_result;
    private LinearLayout ll_top;

    public CustomerServiceFragment(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View ret = inflater.inflate(R.layout.fragment_customer_service,container,false);
        initView(ret);
        return ret;
    }

    private void initView(View ret) {
        ll_top = (LinearLayout) ret.findViewById(R.id.customer_service_top);
        ll_top.setBackground(new BitmapDrawable(BitmapBlurHelper.fastBlur(BitmapFactory.decodeResource(getResources(),R.drawable.mybg),7)));

//        String url = "http://www.365zhuti.com/wall/UploadPic/2012-4/365zhuti_20120418095954_0.jpg";
//        ImageRequest request = new ImageRequest(url,new Response.Listener<Bitmap>() {
//            @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
//            @Override
//            public void onResponse(Bitmap bitmap) {
//
//            }
//        },ll_top.getWidth(),ll_top.getHeight(), Bitmap.Config.ARGB_8888,new Response.ErrorListener() {
//            @Override
//            public void onErrorResponse(VolleyError volleyError) {
//
//            }
//        });
//        MyApplication.getmInstance().getRequestQueue().add(request);

        ListView customerListView = (ListView) ret.findViewById(R.id.customer_list_view);
        customerListView.setAdapter(new CustomerListAdapter(getActivity()));

//        Button btn_search = (Button) ret.findViewById(R.id.customer_service_btn_search);
//        btn_search.setOnClickListener(this);
        ImageView img_voice = (ImageView) ret.findViewById(R.id.customer_voice);
        img_voice.setOnClickListener(this);
        et_result = (EditText) ret.findViewById(R.id.customer_service_et_result);
        mSpeechDialog = new RecognizerDialog(getActivity(), new InitListener() {
            @Override
            public void onInit(int code) {
                if (code != ErrorCode.SUCCESS) {
                    System.out.println("---------->");
                }
            }
        });

        ret.findViewById(R.id.customer_phone).setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.customer_voice:
                mSpeechDialog.setListener(this);
                mSpeechDialog.show();
                break;
            case R.id.customer_phone:
                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + Constant.PHONE));
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onResult(RecognizerResult recognizerResult, boolean b) {
        printResult(recognizerResult);
    }

    @Override
    public void onError(SpeechError speechError) {

    }

    private void printResult(RecognizerResult results) {
        String text = JsonParser.parseIatResult(results.getResultString());

        String sn = null;
        // 读取json结果中的sn字段
        try {
            JSONObject resultJson = new JSONObject(results.getResultString());
            sn = resultJson.optString("sn");
        } catch (JSONException e) {
            e.printStackTrace();
        }

        mIatResults.put(sn, text);

        StringBuffer resultBuffer = new StringBuffer();
        for (String key : mIatResults.keySet()) {
            resultBuffer.append(mIatResults.get(key));
        }

        String temp = resultBuffer.toString();
        temp = temp.replace("。","");

        et_result.setText(temp);
        et_result.setSelection(et_result.length());
    }
}
